VERSION 1.0 CLASS
BEGIN
  MultiUse = -1  'True
END
Attribute VB_Name = "TextSplitter"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = False
Attribute VB_Exposed = True
Option Explicit

Private marrWords() As String
Private mWordCount As Long

Private Sub Reset()
    Erase marrWords
    mWordCount = 0
End Sub

Public Sub LoadText(inText As String, Optional separators As String = " ")
    Reset
    TSSplit inText, separators
End Sub

Public Property Get WordCount() As Long
    WordCount = mWordCount
End Property

Public Function Word(which As Long) As String
    If mWordCount = 0 Then
        Word = ""
    ElseIf which < 1 Or which > mWordCount Then
        Word = ""
    Else
        Word = marrWords(which)
    End If
End Function

Private Sub TSSplit(ByVal sIn As String, sDelim As String)

Dim sRead As String

    sIn = Trim(sIn)
    
    If Len(sIn) = 0 Then
        mWordCount = 0
        Exit Sub
    End If

    sRead = NextDelimitedWord(sIn, sDelim)
    
    Do While sRead <> ""
    
        StoreWord sRead
    
        sRead = NextDelimitedWord(sIn, sDelim)
        
    Loop
    
    StoreWord sIn
    
End Sub

Private Sub StoreWord(newWord As String)

    If newWord = "" Then
        Exit Sub
    End If
    
    mWordCount = mWordCount + 1
    ReDim Preserve marrWords(1 To mWordCount)
    marrWords(mWordCount) = newWord
        
End Sub

Private Function NextDelimitedWord(ByRef searchString As String, delimiters As String) As String

Dim idx As Long
Dim startPos As Long

    idx = 1
    
    Do While IsADelimiter(Mid(searchString, idx, 1), delimiters) And idx <= Len(searchString)
        idx = idx + 1
    Loop
    
    startPos = idx
    
    Do While Not IsADelimiter(Mid(searchString, idx, 1), delimiters) And idx <= Len(searchString)
        idx = idx + 1
    Loop
    
    NextDelimitedWord = Mid(searchString, startPos, idx - startPos)
    
    searchString = Mid(searchString, idx + 1)
    
End Function

Private Function IsADelimiter(theChar As String, delimiters As String)

Dim delimIndex As Long

    For delimIndex = 1 To Len(delimiters)
        If theChar = Mid(delimiters, delimIndex, 1) Then
            IsADelimiter = True
            Exit Function
        End If
    Next
    
    IsADelimiter = False
    
End Function
